home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / ScrapLib / ScrapLib.c next >
Encoding:
C/C++ Source or Header  |  1994-01-02  |  1.8 KB  |  86 lines  |  [TEXT/KAHL]

  1. /* functions for accessing the scrap
  2.     93/12/24 aih added function to return handle to scrap
  3.     93/12/18 aih created */
  4.  
  5. #include <limits.h>
  6. #include "MemoryLib.h"
  7. #include "ScrapLib.h"
  8.  
  9. /* return size of scrap for specified type */
  10. long ScrapLength(ResType type)
  11. {
  12.     long len = 0;
  13.     long offset = 0;
  14.     
  15.     len = GetScrap(NULL, type, &offset);
  16.     if (len < 0) {
  17.         if (len != noTypeErr)
  18.             FailOSErr(len);
  19.         len = 0;
  20.     }
  21.     ensure(len >= 0);
  22.     return(len);
  23. }
  24.  
  25. /* return offset into scrap of specified type */
  26. long ScrapOffset(ResType type)
  27. {
  28.     long len = 0;
  29.     long offset = 0;
  30.     
  31.     require(ScrapLength(type) > 0);
  32.     len = GetScrap(NULL, type, &offset);
  33.     if (len < 0) FailOSErr(len);
  34.     ensure(offset >= 0);
  35.     return(offset);
  36. }
  37.  
  38. /* return actual handle to the scrap data, or NULL if no scrap handle */
  39. Handle ScrapHandle(void)
  40. {
  41.     PScrapStuff scrap = InfoScrap();
  42.     
  43.     if (! scrap->scrapHandle) {
  44.         LoadScrap();
  45.         scrap = InfoScrap();
  46.     }
  47.     return(scrap->scrapHandle);
  48. }
  49.  
  50. /* return copy of scrap data of specified type, or NULL if not in scrap */
  51. Handle ScrapGet(ResType type)
  52. {
  53.     Handle scrap = NULL;
  54.     long offset = 0;
  55.     long len = 0;
  56.     
  57.     len = ScrapLength(type);
  58.     if (len > 0) {
  59.         scrap = HandleBegin(len);
  60.         len = GetScrap(scrap, type, &offset);
  61.         if (len < 0) {
  62.             HandleEnd(scrap);
  63.             FailOSErr(len);
  64.         }
  65.     }
  66.     ensure((! scrap && len == 0) || HandleSize(scrap) == len);
  67.     return(scrap);
  68. }
  69.  
  70. /* return preferred scrap type when application can accept different types,
  71.     or 0 if no data of requested type in scrap (see IM-1, p460) */
  72. ResType ScrapPreferredType(const ResType *types, short ntypes)
  73. {
  74.     long minoffset = LONG_MAX;
  75.     ResType type = 0;
  76.     short i;
  77.     
  78.     for (i = 0; i < ntypes; i++) {
  79.         if (ScrapLength(types[i]) > 0 && ScrapOffset(types[i]) < minoffset) {
  80.             minoffset = ScrapOffset(types[i]);
  81.             type = types[i];
  82.         }
  83.     }
  84.     return(type);
  85. }
  86.